package com.isunland.managesystem.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.isunland.managesystem.R;
import com.isunland.managesystem.entity.FormDetail;
import java.util.List;

public class FormDetailAdapter extends ArrayAdapter<FormDetail> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-06-03 16:03:34 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final LinearLayout rootView;
    public final TextView mTvNameFormDetail;
    public final TextView mTvValueFormDetail;

    private ViewHolder(LinearLayout rootView, TextView mTvNameFormDetail,
        TextView mTvValueFormDetail) {
      this.rootView = rootView;
      this.mTvNameFormDetail = mTvNameFormDetail;
      this.mTvValueFormDetail = mTvValueFormDetail;
    }

    public static ViewHolder create(LinearLayout rootView) {
      TextView mTvNameFormDetail = (TextView) rootView.findViewById(R.id.tv_name_formDetail);
      TextView mTvValueFormDetail = (TextView) rootView.findViewById(R.id.tv_value_formDetail);
      return new ViewHolder(rootView, mTvNameFormDetail, mTvValueFormDetail);
    }
  }

  private static final String TAG = "FormDetailAdapter";

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_form_detail, parent, false);
      vh = ViewHolder.create((LinearLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    FormDetail item = getItem(position);
    String name = item.getName();
    String value = item.getValue();
    String isNec = item.getIsNeccessary();
    if ("true".equalsIgnoreCase(isNec) && isEmpty(value)) {
      isNeccessaryExist = true;
      vh.mTvNameFormDetail.setTextColor(getContext().getResources().getColor(R.color.red));
    } else {
      vh.mTvNameFormDetail.setTextColor(
          getContext().getResources().getColor(android.R.color.primary_text_light));
    }
    vh.mTvNameFormDetail.setText(name);
    vh.mTvValueFormDetail.setText(value);

    return vh.rootView;
  }

  private LayoutInflater mInflater;
  public boolean isNeccessaryExist;//是否存在未填的必填项。

  // Constructors
  public FormDetailAdapter(Context context, List<FormDetail> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  public FormDetailAdapter(Context context, FormDetail[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
  }

  private boolean isEmpty(String value) {
    try {
      return TextUtils.isEmpty(value)
          || value.equalsIgnoreCase("null")
          || Double.parseDouble(value) == 0;
    } catch (NumberFormatException e) {
      e.printStackTrace();
      return false;
    }
  }
}
